﻿using System.Net;
using System.Linq;
using System.Web.Mvc;
using YogaStudio.Data.Domain;
using YogaStudio.Service.BLL;
using YogaStudio.WebUI.Models;

namespace YogaStudio.WebUI.Controllers
{
    public class CustomersController : Controller
    {
        //private YogaStudioEntities db = new YogaStudioEntities();
        ICustomerService customerService = new CustomerService();
         User user;
        UserManagementService userManagement = new UserManagementService();
        IFacultyService facultyService = new FacultyService();
        Faculty _currentFaculty;
        public Faculty CurrentFaculty
        {
            get
            {
                return _currentFaculty ?? (_currentFaculty = facultyService.GetFacultyByLogInUserName(User.Identity.Name));
            }
        }
        // GET: Customers
        public ActionResult Index()
        {
            //var customers = db.Customers.Include(c => c.Faculty).Include(c => c.User);
            user = userManagement.GetUser(User.Identity.Name);
            if (user == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
            Customer customer = customerService.GetCustomerByUserId(user.UserId);
            return View(customer);
        }

        // GET: Customers/Details/5
        public ActionResult Details(int? id)
        {
            ViewBag.AllowEditting = true;
            if (id != null)
            {
                // If anyone view someone's detail, not allow editting
                ViewBag.AllowEditting = false;
                var cus = customerService.GetCustomerByUserId(id.Value);
                return cus != null ? View(cus) : (ActionResult)HttpNotFound();
            }
            user = userManagement.GetUser(User.Identity.Name);
            if (user == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Customer customer = customerService.GetCustomerByUserId(user.UserId);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        // GET: Customers/Create
        public ActionResult Create()
        {
           // ViewBag.FacultyId = new SelectList(db.Faculties, "Id", "Title");
            //ViewBag.UserId = new SelectList(db.Users, "UserId", "UserName");
             user = userManagement.GetUser(User.Identity.Name);
            if (user != null)
            {
                Customer cust = customerService.GetCustomerByUserId(user.UserId);
                if (cust != null)
                {
                    return View(cust);
                }
            }
            return View();
        }

        // POST: Customers/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,FullName,Address,PhoneNumber")] Customer customer)
        {
            // if (ModelState.IsValid)
            // {
            
            FacultyService advisor=new FacultyService();
            Faculty ad=new Faculty();
          
            user = userManagement.GetUser(User.Identity.Name);
            if (user != null)
            {

                ad = advisor.GetFreeFaculty();

                customer.UserId = user.UserId;
            Customer cust = customerService.GetCustomerByUserId(user.UserId);
            if (cust == null)
            {
                customer.FacultyId = ad.Id;
                customerService.SaveCustomer(customer);
            }
            else
            {
                customer.FacultyId = cust.FacultyId;
                customer.UserId = cust.UserId;
                customer.UserId = user.UserId;
                customerService.UpdateCusotmer(customer);
            }
        }

        // return RedirectToAction("Details");
       
            return View("Details");
        }
        
        // GET: Customers/Edit/5
        public ActionResult Edit()
        {
            user = userManagement.GetUser(User.Identity.Name);
            if (user == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Customer customer = customerService.GetCustomerByUserId(user.UserId);
            if (customer == null)
            {
                return HttpNotFound();
            }
           // ViewBag.FacultyId = new SelectList(db.Faculties, "Id", "Title", customer.FacultyId);
           // ViewBag.UserId = new SelectList(db.Users, "UserId", "UserName", customer.UserId);
            return View(customer);
        }

        // POST: Customers/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Customer customer)
        {
           
            if (ModelState.IsValid)
        {
                customerService.UpdateCusotmer(customer);
                return RedirectToAction("Details");
            }
          //  ViewBag.FacultyId = new SelectList(db.Faculties, "Id", "Title", customer.FacultyId);
           // ViewBag.UserId = new SelectList(db.Users, "UserId", "UserName", customer.UserId);
            return View(customer);
        }

        // GET: Customers/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Customer customer = customerService.GetCustomer((int)id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        // POST: Customers/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Customer customer = customerService.GetCustomer((int)id);
            customerService.DeleteCustomer(customer);
            return RedirectToAction("Index");
        }

        [Authorize(Roles="Faculty, Admin")]
        public ActionResult ViewStudents()
        {
            ViewBag.FacultyFullName = CurrentFaculty == null ? "Unknown" : CurrentFaculty.FullName;
            return View();
        }

        [Authorize(Roles = "Faculty, Admin")]
        public ActionResult _Students()
        {
            var currentFaculty = facultyService.GetFacultyByLogInUserName(User.Identity.Name);
            return PartialView(customerService
                .GetStudentsByFacultyId(currentFaculty == null ? (int?)null : currentFaculty.Id)
                .Select(item => (StudentsViewModel)item));
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
               customerService.Dispose(true);
            }
            base.Dispose(disposing);
        }
    }
}
